﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DataAccessLayer;
using System.Data;
namespace iVision
{
    /// <summary>
    /// Interaction logic for SelectCustomer.xaml
    /// </summary>
    public partial class SelectCustomer : PageFunction<String>
    {
        private ListViewFilter filters;

        public SelectCustomer()
        {
            InitializeComponent();
            populateCustomerListView();
            List<ListView> listViews = new List<ListView>();
            listViews.Add(lvCustomers);
           

            filters = new ListViewFilter(listViews);
        }

       
      

        private void populateCustomerListView()
        {
            try
            {
                DataAccessLayer.DataAccessLayer dal = new DataAccessLayer.DataAccessLayer(App.connectionString);
                lvCustomers.ItemsSource = dal.getAllCustomers().DefaultView;
            }
            catch (Exception e)
            {
                MessageBox.Show("An Error Has Occured : " + e.Message);
            }
        }

        private void btnGetCustomer_Click(object sender, RoutedEventArgs e)
        {
            String selectedCustomerID = "0";
            if (lvCustomers.SelectedItem != null)
                selectedCustomerID = ((DataRowView)lvCustomers.SelectedItem)[0].ToString();
            OnReturn(new ReturnEventArgs<string>(selectedCustomerID));
        }

        private void ListView_TextChanged(object sender, TextChangedEventArgs e)
        {
            //Get listView
            ListView lv = (ListView)sender;
            //Get TextBox
            TextBox tb = (TextBox)e.OriginalSource;
            filters.addFilterToListViewFilterList(lv, (String)tb.Tag, tb.Text);
            filters.filterListView(lv);

        }
    }
}
